package com.yazi.mxz.behavior.controller;


import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ListResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.Category;
import com.yazi.mxz.behavior.jackson.filter.CategoryFilter;
import com.yazi.mxz.behavior.repository.CategoryRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("categoryController")
@RequestMapping("/category")
public class CategoryController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CategoryController.class);

	@Resource
	CategoryRepository categoryRepository;

	@RequestMapping(value="/list/json", params={"type"})
	@ResponseBody
	public ListResult<?> listJson(int type) {
		log.debug("获取根类目: ");
		ListResult<Category> jsonResult = new ListResult<Category>();
		jsonResult.setFilter(CategoryFilter.get());
		try{
			jsonResult.setResult(categoryRepository.findByTypeAndDeepOrderBySort(type, 1));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取类目列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping(value="/list/json", params={"parentId"})
	@ResponseBody
	public ListResult<?> listByParentIdJson(long parentId) {
		log.debug("获取子级类目: parentId={}", parentId);
		ListResult<Category> jsonResult = new ListResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());// 包含子子一级
		try{
			jsonResult.setResult(categoryRepository.findByParentIdOrderBySort(parentId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子级类目异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
}
